import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

class RequestParameter{
    public int userId;
    public int userName;
}
@WebServlet("/PostParameter")
public class PostParameter extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream inputStream = req.getInputStream();
        int length = req.getContentLength();
        byte[] buffer = new byte[length];
        inputStream.read(buffer);
        String body = new String(buffer,"utf-8");
        //解析body
        ObjectMapper objectMapper = new ObjectMapper();
        RequestParameter requestParameter = objectMapper.readValue(body,RequestParameter.class);
        resp.getWriter().write(String.format("userId: %d,userName: %d",
                requestParameter.userId,requestParameter.userName));
        //数组打印为ASCII码值
        //resp.getWriter().write(Arrays.toString(buffer));

    }
}
